package cn.zhoutingrui.pethome.dept.controller;

import cn.zhoutingrui.pethome.dept.doamin.Department;
import cn.zhoutingrui.pethome.dept.query.DepartmentQuery;
import cn.zhoutingrui.pethome.dept.service.DepartmentService;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController
@Api(value = "部门的API",description = "部门相关增删改查")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有")
    public AjaxResultUtil findAll(){
        try {
            List<Department> all = departmentService.findAll();
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(all);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.fail();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "通过id查询")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Long")
    public AjaxResultUtil findById(@PathVariable("id")Long id){
        try {
            Department byId = departmentService.findById(id);
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(byId);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.fail();
    }

    @RequestMapping(value = "/pageList",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询")
    public AjaxResultUtil pageList(@RequestBody DepartmentQuery departmentQuery){
        try {
            List<Department> query = departmentService.findQuery(departmentQuery);
            Long count = departmentService.findByQueryCount(departmentQuery);
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(query).setTotal(count);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.fail();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除一条数据")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Long")
    public AjaxResultUtil deleteById(@RequestBody @PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }

    }

    @RequestMapping(method = RequestMethod.PUT)
    @ApiOperation(value = "数据更改[id为空为新增,不为空为修改]")
    public AjaxResultUtil save(@RequestBody Department department){
        try {
            if (department.getId()==null){
                //执行添加
                departmentService.add(department);
            }else {
                //执行修改
                departmentService.update(department);
            }
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }

    }

    @RequestMapping(value = "/dels",method = RequestMethod.PATCH)
    @ApiOperation("批量删除")
    public AjaxResultUtil batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResultUtil.fail();
    }

    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    @ApiOperation("获取部门级树")
    public List<Department> deptTree( Long id){
        System.out.println(id);
        List<Department> depts = departmentService.deptTree(id);
        System.out.println(depts);
        return depts;
    }
}
